package com.miyu.system.utils.page;

import com.alibaba.fastjson2.JSONObject;
import com.miyu.system.utils.stool.SystemStatic;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class PageLimit {

    public static Pageable pageable(JSONObject params) {
        /*当前页*/
        JSONObject page = new JSONObject();
        Integer currentPage= SystemStatic.SYS_PAGE_CURRENT_PAGE;
        Integer pageSize=SystemStatic.SYS_PAGE_PAGE_SIZE;
        if(params.containsKey(SystemStatic.SYS_PAGE_LIMIT)){
            page = params.getJSONObject(SystemStatic.SYS_PAGE_LIMIT)    ;
        }else{
            return PageRequest.of(currentPage,pageSize);
        }
       try{
           if(page.containsKey("currentPage"))
             currentPage = page.getInteger("currentPage");
        /*每页多少条*/
           if(page.containsKey("pageSize"))
             pageSize = page.getInteger("pageSize");
        currentPage = currentPage >1? currentPage-1 : 0;
       } catch (Exception e) {
           throw new RuntimeException(e);
       }
        return PageRequest.of(currentPage,pageSize);
    }

    public static Pageable pageable(JSONObject params,Sort sort) {
        /*当前页*/
        JSONObject page = new JSONObject();
        Integer currentPage= SystemStatic.SYS_PAGE_CURRENT_PAGE;
        Integer pageSize=SystemStatic.SYS_PAGE_PAGE_SIZE;
        if(params.containsKey(SystemStatic.SYS_PAGE_LIMIT)){
            page = params.getJSONObject(SystemStatic.SYS_PAGE_LIMIT)    ;
        }else{
            return PageRequest.of(currentPage,pageSize);
        }
        try{
            if(page.containsKey("currentPage"))
                currentPage = page.getInteger("currentPage");
            /*每页多少条*/
            if(page.containsKey("pageSize"))
                pageSize = page.getInteger("pageSize");
            currentPage = currentPage >1? currentPage-1 : 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return PageRequest.of(currentPage,pageSize,sort);
    }

}
